package com.komodo.island.gdx;

import android.os.PowerManager;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.komodo.island.gdx.dialog.Dialog;
import com.komodo.island.gdx.dialog.LoadingDialog;
import com.komodo.island.gdx.dialog.PauseDialog;
import com.komodo.island.gdx.dialog.StarterDialog;
import com.komodo.island.gdx.screen.CreditScreen;
import com.komodo.island.gdx.screen.GameScreen;
import com.komodo.island.gdx.screen.HelpScreen;
import com.komodo.island.gdx.screen.MainMenuScreen;
import com.komodo.island.gdx.screen.MapScientistScreen;
import com.komodo.island.gdx.screen.MapTouristScreen;
import com.komodo.island.gdx.screen.PlayerSelectionScreen;
import com.komodo.island.gdx.screen.PuzzleScreen;
import com.komodo.island.gdx.screen.SplashScreen;
import com.komodo.island.gdx.screen.StoryScreen;

public class KomodoIsland extends Game {

	/*
	 * The main game ...game loop, render screen, the main class
	 */

	public Input input = new Input();

	public  PowerManager.WakeLock wl;
	
	public MainMenuScreen mainMenuScreen;
	public PlayerSelectionScreen playerScreen;
	public MapScientistScreen scientistScreen;
	public MapTouristScreen touristScreen;

	public CreditScreen creditScreen;
	public PuzzleScreen puzzleScreen;

	public GameScreen gameBalance;
	public GameScreen gameSuit;
	public GameScreen gameKomodoPhoto;
	// public GameScreen gameShopping;
	public GameScreen gameUnderWater;
	public GameScreen gameSandCastle;
	public GameScreen gameKomodoMove;

	private SplashScreen splashScreen;

	public SpriteBatch batch;

	public Texture menuTexture;
	public Texture buttonTexture;
	public Texture dialogTexture;
	// public Texture gamedescTexture;
	public Texture factTexture;
	public Texture additionTexture;

	// public TextureAtlas atlas;

	public Texture helpTexture;
	public TextureRegion puzzlePieces[];

	// public StoryScreen story1;

	public OrthographicCamera camera;

	public StoryScreen storyTourist;
	public StoryScreen storyScientist;
	
	public PauseDialog pauseDialog;

	public TextureRegion[] startupAnimation;

	public HelpScreen helpScreen;

	// public Texture puzzleTexture;

	public Music main;
	public Music underwater;
	public Music castle;
	public Music move;
	public Music photo;
	public Music balance;
	public Music tourist;
	public Music suit;
	public Music scientist;
	public Music puzzle;

	public Sound mainSound;
	public Sound gameMenuSound;
	public Sound dialogSound;
	public Sound mapSound;

	public StarterDialog startDialog;

	public LoadingDialog loadingDialog;

	public void init() {

		// set the input processor for this game......
		input.init(this);
		Gdx.input.setInputProcessor(input);

		Persistence.load();

		// System.out.println("load");
		if (menuTexture == null)
			menuTexture = new Texture(Gdx.files.internal("menu1.png"));
		if (dialogTexture == null)
			dialogTexture = new Texture(Gdx.files.internal("dialog1.png"));

		if (mainSound == null)
			mainSound = Gdx.audio.newSound(Gdx.files
					.internal("sfx/general/mainmenu.mp3"));
		if (gameMenuSound == null)
			gameMenuSound = Gdx.audio.newSound(Gdx.files
					.internal("sfx/general/gamemenu.mp3"));
		if (mapSound == null)
			mapSound = Gdx.audio.newSound(Gdx.files
					.internal("sfx/general/map.mp3"));
		if (dialogSound == null)
			dialogSound = Gdx.audio.newSound(Gdx.files
					.internal("sfx/general/yesno.mp3"));

		if (main == null) {
			main = Gdx.audio.newMusic(Gdx.files.internal("bgm/main.mp3"));
			main.setLooping(true);
			main.setVolume(0.5f);
		}

		if (tourist == null) {
			tourist = Gdx.audio.newMusic(Gdx.files.internal("bgm/tourist.mp3"));
			tourist.setLooping(true);
			tourist.setVolume(0.5f);
		}

		if (scientist == null) {
			scientist = Gdx.audio.newMusic(Gdx.files
					.internal("bgm/scientist.wav"));
			scientist.setLooping(true);
			scientist.setVolume(0.5f);
		}

		if (puzzle == null) {
			puzzle = Gdx.audio.newMusic(Gdx.files.internal("bgm/puzzle.wav"));
			puzzle.setLooping(true);
			puzzle.setVolume(0.5f);
		}

		if (loadingDialog==null){
			loadingDialog=new LoadingDialog(480, 320, true, null, dialogTexture, this);
		}
		if (pauseDialog==null){
			pauseDialog=new PauseDialog(480, 320, true, null, dialogTexture,this);
		}
		
		batch = Art.batch;

		camera = new OrthographicCamera(480, 320);

		if (mainMenuScreen == null) {
			mainMenuScreen = new MainMenuScreen();
			mainMenuScreen.init(this, menuTexture);
		}
		//
		if (additionTexture == null)
			additionTexture = new Texture(Gdx.files.internal("addition1.png"));

		// buttonTexture = new Texture(Gdx.files.internal("button1.png"));
		// dialogTexture = new Texture(Gdx.files.internal("dialog1.png"));
		// gamedescTexture = new Texture(Gdx.files.internal("story1.png"));
		// factTexture = new Texture(Gdx.files.internal("facts1.png"));
		// helpTexture = new Texture(Gdx.files.internal("help1.png"));
		//
		// puzzlePieces = new TextureRegion[96];
		// // atlas = new TextureAtlas(Gdx.files.internal("pack"));
		//
		// // for (int i = 0 ; i<96;i++){
		// // puzzlePieces[i] = atlas.findRegion(""+(i+1));
		// //
		// // }
		// // System.out.println("Finish");
		//
		//
		//
		// //
		// startupAnimation = new TextureRegion[3];
		// startupAnimation[0] = new TextureRegion(menuTexture, 0 ,
		// 320,270,270);
		// startupAnimation[1] = new TextureRegion(menuTexture, 270 ,
		// 320,270,270);
		// startupAnimation[2] = new TextureRegion(menuTexture, 270*2 ,
		// 320,270,270);
		//
		//

		//
		// playerScreen = new PlayerSelectionScreen();
		// playerScreen.init(this, menuTexture);
		//
		// helpScreen =new HelpScreen(null, 1, 5000, null);
		// helpScreen.init(this, helpTexture);
		//
		// scientistScreen = new MapScientistScreen();
		// scientistScreen.init(this, menuTexture);
		//
		// touristScreen = new MapTouristScreen();
		// touristScreen.init(this, menuTexture);
		//
		// puzzleScreen = new PuzzleScreen();
		// puzzleScreen.init(this, menuTexture);
		//
		// creditScreen = new CreditScreen(null, 3, mainMenuScreen);
		// creditScreen.init(this, menuTexture);
		//
		// gameSuit = new SuitKomodo();
		// gameSuit.init(this, menuTexture);
		//
		// gameKomodoPhoto = new KomodoPhoto();
		// gameKomodoPhoto.init(this, menuTexture);
		//
		// gameBalance = new GameBalance();
		// gameBalance.init(this, menuTexture);
		//
		// gameShopping = new ShoppingGame();
		// gameShopping.init(this, menuTexture);
		//
		// gameUnderWater = new KomodoUnderwater();
		// gameUnderWater.init(this, menuTexture);
		//
		// gameSandCastle = new SandCastle();
		// gameSandCastle.init(this, menuTexture);
		//
		// gameKomodoMove = new KomodoMove();
		// gameKomodoMove.init(this, menuTexture);
		//
		// TextureRegion[] scientistStory = { new TextureRegion(menuTexture,
		// 480 * 3, 0, 480, 320) };
		// TextureRegion[] touristStory = { new TextureRegion(menuTexture, 0,
		// 320 + 270, 480, 320) };
		//
		// storyScientist = new StoryScreen(scientistStory, 1, 3000,
		// scientistScreen);
		// storyScientist.init(this ,menuTexture);
		// storyTourist = new StoryScreen(touristStory, 1, 3000, touristScreen);
		// storyTourist.init(this, menuTexture);
		//
		//
		//
		//

		if (startDialog == null) {
			startDialog = new StarterDialog(480, 320, true, mainMenuScreen,
					menuTexture, this);
		}

		if (splashScreen == null) {
			splashScreen = new SplashScreen();
			splashScreen.init(this, menuTexture);
		}

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		super.pause();
		System.gc();
	}

	public void create() {
		// TODO Auto-generated method stub
		// Initialize all object...
		init();

		// the first screen....
		// mainMenuScreen.onScreenLoading();

		setScreen(splashScreen);

	}

	@Override
	public void render() {
		// TODO Auto-generated method stub
		super.render();

	}

	/*
	 * Go to another screen, e.g. go to main menu, go to mini game, etc
	 */

	public void goToScreen(com.komodo.island.gdx.screen.Screen nextScreen) {
		// ((com.komodo.island.gdx.Screen) nextScreen).previousScreen =
		// (com.komodo.island.gdx.Screen) getScreen();
		nextScreen.setScreenToRun();
		setScreen(nextScreen);
	}

	public int getIDCurrentScreen(Screen screen) {
		return ((com.komodo.island.gdx.screen.Screen) screen).ID;
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		super.dispose();

	}

	public void exitAll() {

		Persistence.save();
		// try {
		batch.dispose();
		if (mainMenuScreen != null) {
			mainMenuScreen.cleanResScreen();
			mainMenuScreen.dispose();

		}
		if (playerScreen != null) {
			playerScreen.cleanResScreen();
			playerScreen.dispose();
		}

		if (scientistScreen != null) {
			scientistScreen.cleanResScreen();
			scientistScreen.dispose();
		}
		if (touristScreen != null) {
			touristScreen.cleanResScreen();
			touristScreen.dispose();

		}
		if (helpScreen != null) {
			helpScreen.cleanResScreen();
			helpScreen.dispose();
		}
		if (creditScreen != null) {
			creditScreen.cleanResScreen();
			creditScreen.dispose();
		}
		if (gameBalance != null) {
			gameBalance.cleanResScreen();
			gameBalance.dispose();

		}
		if (gameKomodoPhoto != null) {
			gameKomodoPhoto.cleanResScreen();
			gameKomodoPhoto.dispose();
		}

		if (gameSuit != null) {
			gameSuit.cleanResScreen();
			gameSuit.dispose();
		}
		if (gameKomodoMove != null) {
			gameKomodoMove.cleanResScreen();
			gameKomodoMove.dispose();
		}
		if (gameUnderWater != null) {
			gameUnderWater.cleanResScreen();
			gameUnderWater.dispose();

		}
		if (gameSandCastle != null) {
			gameSandCastle.cleanResScreen();
			gameSandCastle.dispose();
		}

		if (menuTexture != null)
			menuTexture.dispose();
		if (dialogTexture != null)
			dialogTexture.dispose();
		if (buttonTexture != null)
			buttonTexture.dispose();
		if (factTexture != null)
			factTexture.dispose();
		if (helpTexture != null)
			helpTexture.dispose();

		// atlas.dispose();

		if (batch != null)
			batch.dispose();

		if (main != null)
			main.dispose();
		if (puzzle != null)
			puzzle.dispose();
		if (scientist != null)
			scientist.dispose();
		if (tourist != null)
			tourist.dispose();
		if (suit != null)
			suit.dispose();
		if (balance != null)
			balance.dispose();
		if (photo != null)
			photo.dispose();
		if (castle != null)
			castle.dispose();
		if (underwater != null)
			underwater.dispose();
		if (move != null)
			move.dispose();

		if (mainSound != null)
			mainSound.dispose();
		if (dialogSound != null)
			dialogSound.dispose();
		if (mapSound != null)
			mapSound.dispose();
		if (gameMenuSound != null)
			gameMenuSound.dispose();

		if (startDialog != null)
			startDialog.dispose();
		if (loadingDialog!= null)
			loadingDialog.dispose();
		if (pauseDialog != null)
			pauseDialog.dispose();
		// } catch (Exception e) {

		// }
		Art.tulisanTexture.dispose();
	}

}
